/**
 * 
 */
package cn.pfcg.rally.persistent.dao.impl;


import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.pfcg.rally.domain.bean.Administrator;
import cn.pfcg.rally.persistent.dao.AdministratorDAO;

/**
 * @author cxq
 *
 */
public class AdministratorDAOImpl implements AdministratorDAO {

	private HibernateTemplate hibernateTemplate;
	/* (non-Javadoc)
	 * @see cn.pfcg.rally.persistent.dao.AdministratorDAO#getAdministrator(java.lang.String)
	 */
	public Administrator getAdministrator(String username) {
		return (Administrator)hibernateTemplate.find("from Administrator administrator " + "where administrator.username = ?" , username).get(0);
	}

	/**
	 * @param hibernateTemplate the hibernateTemplate to set
	 */
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void deleteAdministrator(String username) {
		Administrator admin = (Administrator)hibernateTemplate.find("from Administrator administrator " + "where administrator.username = ?" , username).get(0);
		hibernateTemplate.delete(admin);
		
	}

	public List getAllAdministrator() {
		return hibernateTemplate.find("from Administrator");
	}

	public void saveAdministrator(Administrator administrator) {
		hibernateTemplate.save(administrator);
	}

	public void updateAdministrator(String username,Administrator administrator) {
		Administrator admin = (Administrator)hibernateTemplate.find("from Administrator administrator " + "where administrator.username = ?" , username).get(0);
		administrator.setId(admin.getId());
		administrator.setTestQuestions(admin.getTestQuestions());
		administrator.setTestPapers(admin.getTestPapers());
		hibernateTemplate.update(administrator);
	}

}
